<?php

class My_storeApp extends StoreadminbaseApp {

    var $_store_id;
    var $_store_mod;
    var $_uploadedfile_mod;

    function __construct() {
        $this->My_storeApp();
    }

    function My_storeApp() {
        parent::__construct();
        $this->_store_id = intval($this->visitor->get('manage_store'));
        $this->_store_mod = & m('store');
        $this->_uploadedfile_mod = &m('uploadedfile');
    }

    function index() {
        $tmp_info = $this->_store_mod->get(array(
            'conditions' => $this->_store_id,
            'join' => 'belongs_to_sgrade',
            'fields' => 'domain, functions',
        ));
        $functions = $tmp_info['functions'] ? explode(',', $tmp_info['functions']) : array();
        $subdomain_enable = false;
        if (ENABLED_SUBDOMAIN && in_array('subdomain', $functions)) {
            $subdomain_enable = true;
        }
        if (!IS_POST) {
            //传给iframe参数belong, item_id
            $this->assign('belong', BELONG_STORE);
            $this->assign('id', $this->_store_id);

            $store = $this->_store_mod->get_info($this->_store_id);
            foreach ($functions as $k => $v) {
                $store['functions'][$v] = $v;
            }

            $this->assign('store', $store);
            $this->assign('editor_upload', $this->_build_upload(array(
                        'obj' => 'EDITOR_SWFU',
                        'belong' => BELONG_STORE,
                        'item_id' => $this->_store_id,
                        'button_text' => Lang::get('bat_upload'),
                        'button_id' => 'editor_upload_button',
                        'progress_id' => 'editor_upload_progress',
                        'upload_url' => 'index.php?app=swfupload',
                        'if_multirow' => 1,
            )));

            extract($this->_get_theme());
            $this->assign('build_editor', $this->_build_editor(array(
                        'name' => 'description',
                        'content_css' => SITE_URL . "/themes/store/{$template_name}/styles/{$style_name}" . '/shop.css', // for preview
            )));

            $region_mod = & m('region');
            $this->assign('regions', $region_mod->get_options(0));
            
            /* 属于店铺的附件 */
            $files_belong_store = $this->_uploadedfile_mod->find(array(
                'conditions' => 'store_id = ' . $this->visitor->get('manage_store') . ' AND belong = ' . BELONG_STORE . ' AND item_id =' . $this->visitor->get('manage_store'),
                'fields' => 'this.file_id, this.file_name, this.file_path',
                'order' => 'add_time DESC'
            ));
            /* 当前页面信息 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('my_store'));
            $this->_curitem('my_store');
            $this->_curmenu('my_store');
            $this->import_resource('jquery.plugins/jquery.validate.js,mlselection.js');
            $this->assign('files_belong_store', $files_belong_store);
            $this->assign('subdomain_enable', $subdomain_enable);
            $this->assign('domain_length', Conf::get('subdomain_length'));
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('my_store'));
            $this->display('my_store.index.html');
        } else {
            $subdomain = $tmp_info['domain'];
            if ($subdomain_enable && !$tmp_info['domain']) {
                $subdomain = empty($_POST['domain']) ? '' : trim($_POST['domain']);
                if (!$this->_store_mod->check_domain($subdomain, Conf::get('subdomain_reserved'), Conf::get('subdomain_length'))) {
                    $this->show_warning($this->_store_mod->get_error());

                    return;
                }
            }
            $data = $this->_upload_files();
            if ($data === false) {
                return;
            } else { //删除冗余图标
                if ($store['store_logo'] != '' && $data['store_logo'] != '') {
                    $store_logo_old = pathinfo($store['store_logo']);
                    $store_logo_new = pathinfo($data['store_logo']);
                    if ($store_logo_old['extension'] != $store_logo_new['extension']) {
                        unlink($store['store_logo']);
                    }
                }

                if ($store['store_banner'] != '' && $data['store_banner'] != '') {
                    $store_banner_old = pathinfo($store['store_banner']);
                    $store_banner_new = pathinfo($data['store_banner']);
                    if ($store_banner_old['extension'] != $store_banner_new['extension']) {
                        unlink($store['store_banner']);
                    }
                }
            }

            $data = array_merge($data, array(
                'store_name' => $_POST['store_name'],
                'region_id' => $_POST['region_id'],
                'region_name' => $_POST['region_name'],
                'description' => $_POST['description'],
                'address' => $_POST['address'],
                'tel' => $_POST['tel'],
                'im_qq' => $_POST['im_qq'],
                'im_ww' => $_POST['im_ww'],
                'service_begin' => $_POST['service_begin'],
                'service_end' => $_POST['service_end'],
                'service_arrive' => $_POST['service_arrive'],
                'service_consumption' => $_POST['service_consumption'],
                'domain' => $subdomain,
                'enable_groupbuy' => $_POST['enable_groupbuy'],
                'business_scope' => trim($_POST['business_scope']),
                    // end 
                'enable_free_fee' => $_POST['enable_free_fee'],
                'amount_for_free_fee' => abs(floatval($_POST['amount_for_free_fee'])),
                'acount_for_free_fee' => intval($_POST['acount_for_free_fee']),
            ));
            $this->_store_mod->edit($this->_store_id, $data);

            $this->show_message('edit_ok');
        }
    }

    function _get_member_submenu() {
        return array(
            array(
                'name' => 'my_store',
                'url' => 'index.php?app=my_store',
            ),
            array(
                'name' => 'callcenter',
                'url'  => 'index.php?app=my_store&act=callcenter',
            ),
            array(
                'name' => 'location',
                'url' => 'index.php?app=my_store&amp;act=location',
            ),
            array(
                'name' => 'store_tuijian_buyer_ratio',
                'url' => 'index.php?app=my_store&act=store_tuijian_buyer_ratio',
            ),
        );
    }

    function location() {
        $store_mod = & m('store');

        if (!IS_POST) {
            $store_info = $store_mod->get($this->_store_id);

            if ($store_info['lng'] == '0' || $store_info['lat'] == '0') {
                //根据IP 获取经纬度
                $data = get_ip_location();
                $store_info = array_merge($store_info, $data);
            }

            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('location'));
            /* 当前用户中心菜单 */
            $this->_curitem('my_profile');
            /* 当前所处子菜单 */
            $this->_curmenu('location');
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('location'));

            $this->assign('baidu_ak', Conf::get('baidu_ak'));
            $this->assign('store_info', $store_info);
            $this->display('my_store.location.html');
        } else {
            $data = array(
                'lng' => $_POST['lng'],
                'lat' => $_POST['lat'],
                'zoom' => $_POST['zoom'],
            );
            $store_mod->edit($this->_store_id, $data);
            $this->show_message('edit_ok');
        }
    }

    /**
     * 上传文件
     *
     */
    function _upload_files() {
        import('uploader.lib');
        $data = array();
        /* store_logo */
        $file = $_FILES['store_logo'];
        if ($file['error'] == UPLOAD_ERR_OK && $file != '') {
            $uploader = new Uploader();
            $uploader->allowed_type(IMAGE_FILE_TYPE);
            $uploader->allowed_size(SIZE_STORE_LOGO); // 20KB
            $uploader->addFile($file);
            if ($uploader->file_info() === false) {
                $this->show_warning($uploader->get_error());
                return false;
            }
            $uploader->root_dir(ROOT_PATH);
            $data['store_logo'] = $uploader->save('data/files/store_' . $this->_store_id . '/other', 'store_logo');
        }

        /* store_banner */
        $file = $_FILES['store_banner'];
        if ($file['error'] == UPLOAD_ERR_OK && $file != '') {
            $uploader = new Uploader();
            $uploader->allowed_type(IMAGE_FILE_TYPE);
            $uploader->allowed_size(SIZE_STORE_BANNER); // 200KB
            $uploader->addFile($file);
            if ($uploader->file_info() === false) {
                $this->show_warning($uploader->get_error());
                return false;
            }
            $uploader->root_dir(ROOT_PATH);
            $data['store_banner'] = $uploader->save('data/files/store_' . $this->_store_id . '/other', 'store_banner');
        }

        $file = $_FILES['wap_banner'];
        if ($file['error'] == UPLOAD_ERR_OK && $file != '') {
            $uploader = new Uploader();
            $uploader->allowed_type(IMAGE_FILE_TYPE);
            $uploader->allowed_size(SIZE_STORE_BANNER); // 200KB
            $uploader->addFile($file);
            if ($uploader->file_info() === false) {
                $this->show_warning($uploader->get_error());
                return false;
            }
            $uploader->root_dir(ROOT_PATH);
            $data['wap_banner'] = $uploader->save('data/files/store_' . $this->_store_id . '/other', 'wap_banner');
        }
        return $data;
    }

    /* 异步删除附件 */

    function drop_uploadedfile() {
        $file_id = isset($_GET['file_id']) ? intval($_GET['file_id']) : 0;
        $file = $this->_uploadedfile_mod->get($file_id);
        if ($file_id && $file['store_id'] == $this->visitor->get('manage_store') && $this->_uploadedfile_mod->drop($file_id)) {
            $this->json_result('drop_ok');
            return;
        } else {
            $this->json_error('drop_error');
            return;
        }
    }
    
    function store_tuijian_buyer_ratio() {
        //未开启店铺分成
        if (!Conf::get('tuijian_buyer_status')) {
            $this->show_warning('店铺未开启推广分成功能', 'back_list', 'index.php?app=my_store');
            exit;
        }
        $system_tuijian_buyer_ratio1 = Conf::get('tuijian_buyer_ratio1'); #系统设置的佣金比例
        $system_tuijian_buyer_ratio2 = Conf::get('tuijian_buyer_ratio2'); #系统设置的佣金比例
        $system_tuijian_buyer_ratio3 = Conf::get('tuijian_buyer_ratio3'); #系统设置的佣金比例
        if (!IS_POST) {
            $store = $this->_store_mod->get_info($this->_store_id);
            
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('store_tuijian_buyer_ratio'));
            $this->_curitem('my_store');
            $this->_curmenu('store_tuijian_buyer_ratio');
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('store_tuijian_buyer_ratio'));
            
            //载入系统默认的配置
            $store['tuijian_buyer_ratio1'] = $system_tuijian_buyer_ratio1;
            $store['tuijian_buyer_ratio2'] = $system_tuijian_buyer_ratio2;
            $store['tuijian_buyer_ratio3'] = $system_tuijian_buyer_ratio3;
            $this->assign('store', $store);
            $this->display('my_store.store_tuijian_buyer_ratio.html');
        } else {
            $store_tuijian_buyer_ratio1 = floatval($_POST['store_tuijian_buyer_ratio1']);
            $store_tuijian_buyer_ratio2 = floatval($_POST['store_tuijian_buyer_ratio2']);
            $store_tuijian_buyer_ratio3 = floatval($_POST['store_tuijian_buyer_ratio3']);
            if ($store_tuijian_buyer_ratio1 + $store_tuijian_buyer_ratio2 + $store_tuijian_buyer_ratio3 > 100) {
                $this->show_warning('比例之和不能大于100', 'back_list', 'index.php?app=my_store&act=store_tuijian_buyer_ratio');
                return;
            }
            //当店铺设置的数值少于系统的数值，则自动载入系统的数值
            if ($store_tuijian_buyer_ratio1 < $system_tuijian_buyer_ratio1) {
                $data['store_tuijian_buyer_ratio1'] = $system_tuijian_buyer_ratio1;
            } else {
                $data['store_tuijian_buyer_ratio1'] = $store_tuijian_buyer_ratio1;
            }
            if ($store_tuijian_buyer_ratio2 < $system_tuijian_buyer_ratio2) {
                $data['store_tuijian_buyer_ratio2'] = $system_tuijian_buyer_ratio2;
            } else {
                $data['store_tuijian_buyer_ratio2'] = $store_tuijian_buyer_ratio2;
            }
            if ($store_tuijian_buyer_ratio3 < $system_tuijian_buyer_ratio3) {
                $data['store_tuijian_buyer_ratio3'] = $system_tuijian_buyer_ratio3;
            } else {
                $data['store_tuijian_buyer_ratio3'] = $store_tuijian_buyer_ratio3;
            }
            if ($data['store_tuijian_buyer_ratio1'] + $data['store_tuijian_buyer_ratio2'] + $data['store_tuijian_buyer_ratio3'] > 100) {
                $this->show_warning('比例之和不能大于100', 'back_list', 'index.php?app=my_store&act=store_tuijian_buyer_ratio');
                return;
            }
            $this->_store_mod->edit($this->_store_id, $data);
            $this->show_message('edit_ok');
        }
    }
    
    function callcenter()
    {
        if (!IS_POST){
            $store = $this->_store_mod->get_info($this->_store_id);
            $store['pre_connects'] = json_decode($store['pre_connects'],true);
            if(!empty($store['pre_connects'])){
                foreach ($store['pre_connects'] as $key => $value) {
                    $store['pre_connects'][$key]['name'] = urldecode($value['name']);
                    $store['pre_connects'][$key]['num'] = urldecode($value['num']);
                }
            }
            $store['after_connects'] = json_decode($store['after_connects'],true);
            if(!empty($store['after_connects'])){
                foreach ($store['after_connects'] as $key => $value) {
                    $store['after_connects'][$key]['name'] = urldecode($value['name']);
                    $store['after_connects'][$key]['num'] = urldecode($value['num']);
                }
            }
            $this->assign('store', $store);
            
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('callcenter'));
            $this->_curitem('my_store');
            $this->_curmenu('callcenter');
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('callcenter'));
            
            $this->display('my_store.callcenter.html');
        }else{
            $pre = $_POST['pre'];
            if(!empty($pre)){
                foreach ($pre as $key => $value) {
                    if(empty($value['name'])||(!in_array($value['type'],array('1','2')))||empty($value['num'])){
                        unset($pre[$key]);
                    }else{
                        //存储为中文
                        $pre[$key]['name'] = urlencode($value['name']);
                        $pre[$key]['num'] = urlencode($value['num']);
                    }
                }
            }
            $data['pre_connects'] = ecm_json_encode($pre);
            $after = $_POST['after'];
            if(!empty($after)){
                foreach ($after as $key => $value) {
                    if(empty($value['name'])||(!in_array($value['type'],array('1','2')))||empty($value['num'])){
                        unset($after[$key]);
                    }else{
                        //存储为中文
                        $after[$key]['name'] = urlencode($value['name']);
                        $after[$key]['num'] = urlencode($value['num']);
                    }
                }
            }
            $data['after_connects'] = ecm_json_encode($after);
            $this->_store_mod->edit($this->_store_id, $data);
            $this->show_message('edit_ok');
        }
    }
}
?>
